<template>
  <Filing v-bind:queryParam="queryParam" refreshUrl="onepiecefiling" v-bind:data="data" v-on:filing="filing()" v-on:initData="initData"></Filing>
</template>

<script>
  import http from '../utils/http.js'
  import store from '../utils/store.js'
  import common from '../utils/common.js'
  import Filing from './Filing'
  export default {
    name: 'OnepieceFiling',
    components: {
      Filing,
    },
    methods: {
      init() {
        this.updateQueryParam(this.$route.params.type, this.$route.params.tagId, this.$route.params.pageNum,
                              this.$route.params.pageSize, this.$route.params.index, this.$route.params.sessionId);
        this.initData();
      },
      updateQueryParam(type, tagId, pageNum, pageSize, index, sessionId) {
        this.queryParam.type = type ? type : this.queryParam.type;
        this.queryParam.tagId = tagId ? tagId : this.queryParam.tagId;
        this.queryParam.pageNum = pageNum ? parseInt(pageNum) : this.queryParam.pageNum;
        this.queryParam.pageSize = pageSize ? pageSize : this.queryParam.pageSize;
        this.queryParam.index = parseInt(index);
        this.queryParam.sessionId = sessionId ? sessionId : this.queryParam.sessionId;
        this.queryParam.fromModule = "Discovery";
      },
      initData() {
        // 发送请求
        let that = this;
        http.post('/filing/detail', this.queryParam, (responseData) => {
          that.filingParam.onePieceId = responseData.onePieceId;
          if (responseData.dir) {
            that.data.filename.filename = responseData.name;
          } else {
            let splitResult = common.splitFilenameExtension(responseData.name);
            that.data.filename.filename = splitResult[0];
            that.data.filename.extension = splitResult[1];
          }
          
          let imageUrls = [];
          for (let imageCode of responseData.imageCodes) {
            imageUrls.push(IMAGE_URL + imageCode);
          }
          that.data.image.imageUrls = imageUrls;
          that.data.image.dimension = responseData.dimension;
          that.data.isDir = responseData.dir;
          that.data.hasNext = responseData.hasNext;
          that.queryParam.sessionId = responseData.sessionId;

          let selectedTags = {}
          responseData.tag.forEach(tag => {
            selectedTags[tag.id] = tag.completeName;
          })
          that.data.tag.selectedTags = selectedTags;
          that.data.needSavaChange = false;
        })
      },
      /**
       * 整理，调用整理的接口
       * */
      filing() {
        let that = this;
        this.filingParam.name = this.data.filename.filename + (this.data.filename.extension ? this.data.filename.extension : '');
        let tagIds = [];
        for (let tagId in this.data.tag.selectedTags) {
          tagIds.push(tagId);
        }
        this.filingParam.tagId = tagIds;
        http.put('/one-piece', this.filingParam, data => {
          that.data.needSavaChange = false;
        })
        for (let i = 0; i < tagIds.length; i++) {
          let tagId = tagIds[i];
          store.recentUsingTagManager.addCount(tagId, this.data.tag.selectedTags[tagId]);
        }
      },
    },
    watch: {
      '$route' (to, from) {
        this.init();
      }
    },
    mounted() {
      this.init();
    },
    data() {
      return {
        queryParam: {
          type: null,
          tagId: null,
          pageNum: null,
          pageSize: null,
          index: null,
          sessionId: null
        },
        filingParam: {
          onePieceId: null,
          name: null,
          tagId: null,
        },
        data: {
          isDir: null,
          hasNext: null,
          image: {
            dimension: {
              width: 0,
              height: 0
            }, // 防止第一次渲染时数据未返回导致的报错
            current: 0,
            imageUrls: null
          },
          filename: {
            filename: null,
            extension: null
          },
          tag: {
            selectedTags: {},
          },
          needSavaChange: false
        }
      }
    }
  }
</script>

<style>
</style>
